﻿using HLStock.Common;
using HLStock.Model;
using HLStock.WmsApi;
using HLStock.WmsApi.AuthenticationInjection;
using HLStock.WmsService;
using Microsoft.AspNetCore.Mvc;

namespace HLStock.Controllers
{
    public class StockRecordController : MyBaseController<StockRecordModel>
    {
        protected override BaseServiceProvider<StockRecordModel> getSp()
        {
            return ServiceLoader.GetService<StockRecordServiceProvider>();
        }
        [HttpPost, HttpGet, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public ResponseData<StockRecordModel> SearchByKeyword(string keyword)
        {
            return new ResponseData<StockRecordModel>((getSp() as StockRecordServiceProvider).SearchByKeyword(keyword));
        }
        [HttpPost, HttpGet, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public ResponseData<StockRecordModel> ListOutRecord(string pk)
        {
            return new ResponseData<StockRecordModel>((getSp() as StockRecordServiceProvider).ListOutRecord(pk));
        }
        [HttpPost, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public ResponseData<StockRecordModel> StockIn(dynamic data)
        {
            string placeId = data.place_id;
            int qty = data.qty;
            string qrcode = data.sub_order_code;
            StockRecordModel stockRecordModel = new StockRecordModel();
            stockRecordModel.QrCode = qrcode;
            stockRecordModel.PlaceId = placeId;
            stockRecordModel.Type = StockType.In;
            stockRecordModel.Qty = qty;
            stockRecordModel.OnHand = qty;
            Save(stockRecordModel);
            return new ResponseData<StockRecordModel>(stockRecordModel);
        }
        [HttpPost, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public ResponseData<StockRecordModel> StockOut(dynamic data)
        {
            string src = data.src;
            int qty = data.qty;
            StockRecordModel stockRecordModel = new StockRecordModel();
            stockRecordModel.Type = StockType.Out;
            stockRecordModel.Qty = qty;
            stockRecordModel.SrcRecord = src;
            Save(stockRecordModel);
            return new ResponseData<StockRecordModel>(stockRecordModel);
        }
    }
}
